package com.qf.hospital.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.hospital.dto.SysMenuDTO;
import com.qf.hospital.entity.SysMenu;
import com.qf.hospital.enums.HospitalExceptionEnums;
import com.qf.hospital.service.SysMenuService;
import com.qf.hospital.vo.LayUIDTreeVO;
import com.qf.hospital.vo.R;
import com.qf.hospital.vo.SysMenuVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ASUS
 * @version V1.0
 * @Project hospital
 * @Package com.qf.hospital.controller
 * @Date 2021/10/19 14:51
 */
@RestController
public class MenuController {

    private Logger log = LoggerFactory.getLogger(MenuController.class);

    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 查询左侧的树结构
     * @param spread
     * @return
     */
    @PostMapping("/sel/loadMenuMangerLeftTreeJson")
    public R loadMenuMangerLeftTreeJson(Integer spread){
        // 将spread当做标识，如果传递了spread，只查询一级和二级菜单返回结果，应用于添加&修改
        //查询所有菜单
        List<SysMenu> sysMenuList = sysMenuService.findAll();
        ArrayList<LayUIDTreeVO> data = new ArrayList<>();
        if(spread != null){
            //只查询一级和二级菜单
            for (SysMenu sysMenu : sysMenuList) {
                if(sysMenu.getParentId() == 0 || sysMenu.getParentId() == 1){
                    LayUIDTreeVO layUIDTreeVO = new LayUIDTreeVO();
                    BeanUtils.copyProperties(sysMenu,layUIDTreeVO);
                    data.add(layUIDTreeVO);
                }
            }
        }else {
            for (SysMenu sysMenu : sysMenuList) {
                LayUIDTreeVO layUIDTreeVO = new LayUIDTreeVO();
                BeanUtils.copyProperties(sysMenu,layUIDTreeVO);
                data.add(layUIDTreeVO);
            }
        }
        return R.ok(data);
    }

    /**
     * 展示table数据
     * @param title
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/sel/queryMenuAllList")
    public R queryMenuAllList(String title,Integer id,
                              @RequestParam(defaultValue = "1") Integer page,
                              @RequestParam(defaultValue = "10")Integer limit){
        //1、查询数据
        PageInfo<SysMenu> pageInfo = sysMenuService.queryMenuAllList(title,id,page,limit);
        //2、封装数据
        long count = pageInfo.getTotal();
        ArrayList<SysMenuVO> sysMenuVOS = new ArrayList<>();
        for (SysMenu sysMenu : pageInfo.getList()) {
            SysMenuVO sysMenuVO = new SysMenuVO();
            BeanUtils.copyProperties(sysMenu,sysMenuVO);
            sysMenuVOS.add(sysMenuVO);
        }
        //3、返回结果
        return R.ok(count,sysMenuVOS);
    }


    /**
     * 添加菜单
     * @return
     */
    @PostMapping("/sel/addMenu")
    public R addMenu(SysMenuDTO sysMenuDTO){
        SysMenu sysMenu = new SysMenu();
        BeanUtils.copyProperties(sysMenuDTO,sysMenu);
        sysMenuService.addMenu(sysMenu);
        return R.ok("添加菜单成功");
    }

    /**
     * 判断是否有子节点
     * @param id
     * @return
     */
    @PostMapping("/sel/checkMenuHasChildren")
    public R checkMenuHasChildren(Integer id){
        if(id == null || id <= 0){
            log.info("【删除权限】 参数不合法！");
            return R.error(HospitalExceptionEnums.REQUEST_PARAMETER_ERROR,"");
        }
        Boolean b = sysMenuService.checkMenuHasChildren(id);
        return b ? R.ok() : R.error(1,"");
    }

    /**
     * 修改菜单
     * @return
     */
    @PostMapping("/sel/updateMenu")
    public R updateMenu(SysMenuDTO sysMenuDTO){
        SysMenu sysMenu = new SysMenu();
        BeanUtils.copyProperties(sysMenuDTO,sysMenu);
        sysMenuService.updateMenu(sysMenu);
        return R.ok("修改菜单成功");
    }


    @PostMapping("/sel/deleteMenu")
    public R deleteMenu(Integer id){
        if(id == null || id <= 0){
            log.info("【删除权限】 参数不合法！");
            return R.error(HospitalExceptionEnums.REQUEST_PARAMETER_ERROR,"");
        }
        sysMenuService.deleteMenu(id);
        return R.ok();
    }

}
